const storage = (type: "session" | "local") => ({
  set(key: string, value: unknown) {
    const storage = type === "session" ? sessionStorage : localStorage;
    storage.setItem(key, JSON.stringify(value));
  },
  get(key: string) {
    const storage = type === "session" ? sessionStorage : localStorage;
    const value = storage.getItem(key);
    if (!value) return "";
    try {
      return JSON.parse(value);
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
      return value;
    }
  },
  remove(key: string) {
    const storage = type === "session" ? sessionStorage : localStorage;
    storage.removeItem(key);
  },
  clear() {
    const storage = type === "session" ? sessionStorage : localStorage;
    storage.clear();
  },
});

export const sessionStore = storage("session");
export default storage("local");
